* Generates:
* 	main table (table 3)
* 	robustness: alternative productivity measures (table C1)
* 	robustness: alternative clustering Conley SE (table C3)
* 	robustness: religion groups and language majority f.e. (table C4)

*******************************Table 3 MAIN TABLE *******************************
			
**************************** CALLS ****************************
use "$data/calls_cell_input.dta", clear
	keep if cells_50km_stateborders	
		
		gen post = (year > 2007)
		egen t = group(year)
		
		** gen interaction term of interest
		gen lang_control_post = lang_control * post
		label var lang_control_post "Lang Control * Post"
		
		reghdfe calls_all_per100C lang_control_post c.share_off_speakers#i.t if cells_50km_stateborders == 1, a(i.id i.subdist_border_id##i.t c.sharecrop_all_fao#i.t c.median_DIST_TOWN#i.t i.district_MCA_s##i.t c.POST_OFF#i.t i.majority_lang_group#i.t) cluster(tahsil_id)
		
		gen in_sample = e(sample)
		
		sum calls_all_per100C calls_tech_per100C calls_others_per100C if in_sample == 1
		unique majority_lang_group if in_sample == 1 & !missing(majority_lang_group)
		
 
* Residualize
foreach var in calls_all_per100C calls_tech_per100C calls_others_per100C lang_control_post {
	reghdfe `var' c.share_off_speakers#i.t if cells_50km_stateborders == 1 & in_sample == 1, a(i.id i.subdist_border_id##i.t c.sharecrop_all_fao#i.t c.median_DIST_TOWN#i.t i.district_MCA_s##i.t c.POST_OFF#i.t i.majority_lang_group#i.t)  residuals(r_`var') cluster(tahsil_id)
}

******* Table 3
local dist 10
global alt_cutoff 25 50 100 150 // alternative Conley SE cutoff for table C.3
local replace replace 

foreach var in calls_all_per100C calls_tech_per100C calls_others_per100C {
	sum `var' if in_sample == 1
	local control_mean = round(`r(mean)', 0.001)
	
	reghdfe `var' lang_control_post c.share_off_speakers#i.t if cells_50km_stateborders == 1 & in_sample == 1, a(i.id i.subdist_border_id##i.t c.sharecrop_all_fao#i.t c.median_DIST_TOWN#i.t i.district_MCA_s##i.t c.POST_OFF#i.t i.majority_lang_group#i.t) cluster(tahsil_id)
	local R2 = round(e(r2), 0.001)
	
	label var r_lang_control_post "Non-state official language speakers (%) * Post"
	label var r_calls_all_per100C "All calls"
	label var r_calls_tech_per100C "Tech calls"
	label var r_calls_others_per100C "Other calls"
	
	* Conley SE
	ols_spatial_HAC r_`var' r_lang_control_post if cells_50km_stateborders == 1, lat(latitude) lon(longitude) timevar(year) panelvar(id) dist(`dist') dropvar bartlett
	
	outreg2 using "$results/tables/table_3.tex", keep(r_lang_control_post) tex(frag) dec(3) bracket label nor2 addtext(R-squared, "`R2'", Mean, "`control_mean'" , Cell f.e., Yes, Subdistrict border * Time f.e., Yes, District * Time f.e., Yes, Language * Time f.e., Yes) nocons nonotes stats(coef se) `replace'
	local replace append 
	
	* Table C.3
	if "`var'" == "calls_all_per100C" {
	** Table C.3: baseline
	outreg2 using "$results/tables/table_C3_`dist'.tex",  keep(r_lang_control_post) tex(frag) dec(3) bracket label nor2 addtext(R-squared, "`R2'", Mean, "`control_mean'" , Cell f.e., Yes, Subdistrict border * Time f.e., Yes, District * Time f.e., Yes, Language * Time f.e., Yes) nocons nonotes stats(coef se) asterisk(se) replace
	
	** save a seperate version for the baseline (RD = 50km)
	outreg2 using "$results/tables/table_C2_50km.tex",  keep(r_lang_control_post) tex(frag) dec(3) bracket label nor2 addtext(R-squared, "`R2'", Mean, "`control_mean'" , Cell f.e., Yes, Subdistrict border * Time f.e., Yes, District * Time f.e., Yes, Language * Time f.e., Yes) nocons nonotes stats(coef se) replace
	
	** Table C.3: alternative cutoff
	foreach cutoff in $alt_cutoff {  
	
		ols_spatial_HAC r_`var' r_lang_control_post if cells_50km_stateborders == 1, lat(latitude) lon(longitude) timevar(year) panelvar(id) dist(`cutoff') dropvar bartlett
		
		outreg2 using "$results/tables/table_C3_`cutoff'.tex",  keep(r_lang_control_post) tex(frag) dec(3) bracket label nor2 addtext(R-squared, "`R2'", Mean, "`control_mean'" , Cell f.e., Yes, Subdistrict border * Time f.e., Yes, District * Time f.e., Yes, Language * Time f.e., Yes) nocons nonotes stats(coef se) asterisk(se) replace
		
		}
	}	
}


*********************** TECH ADOPTION *************************
use "$data/tech_cell_input.dta", clear
keep if cells_50km_stateborders

assert year > 1996
egen t = group(year)

xtset id t

gen post = (year > 2007)
sum post if year < 2008

gen lang_control_post = lang_control * post

reghdfe hyv_share lang_control_post c.share_off_speakers#i.t if cells_50km_stateborders == 1 & dhyv_share2011 !=. & dhyv_share2016 !=. [aw=pop2001], a(i.id i.subdist_border_id##i.t c.sharecrop_all_fao#i.t c.median_DIST_TOWN#i.t i.district_MCA_s##i.t c.POST_OFF#i.t i.majority_lang_group#i.t) cluster(tahsil_id)

gen in_sample = e(sample)
unique majority_lang_group if in_sample == 1 & !missing(majority_lang_group)

foreach var in hyv_share fert_share_total irr_share_total lang_control_post {
	
	reghdfe `var' c.share_off_speakers#i.t if cells_50km_stateborders == 1 & dhyv_share2011 !=. & dhyv_share2016 !=. & in_sample == 1 [aw=pop2001], a(i.id i.subdist_border_id##i.t c.sharecrop_all_fao#i.t c.median_DIST_TOWN#i.t i.district_MCA_s##i.t c.POST_OFF#i.t i.majority_lang_group#i.t) residuals(r_`var') cluster(tahsil_id)
	
	replace r_`var' = sqrt(pop2001) * r_`var'
	
}

* Table 3
local replace append 
local dist 10

foreach var in hyv_share fert_share_total irr_share_total {
	
	sum `var' if year <= 2006 [aw=pop2001]
	local control_mean = round(`r(mean)', 0.001)
	
	* R2
	reghdfe `var' lang_control_post c.share_off_speakers#i.t if cells_50km_stateborders == 1 & dhyv_share2011 !=. & dhyv_share2016 !=. & in_sample == 1 [aw=pop2001], a(i.id i.subdist_border_id##i.t c.sharecrop_all_fao#i.t c.median_DIST_TOWN#i.t i.district_MCA_s##i.t c.POST_OFF#i.t i.majority_lang_group#i.t)  cluster(tahsil_id) 
	local R2 = round(e(r2), 0.001) 

	label var r_lang_control_post "Non-state official language speakers (%) * Post"
	label var r_hyv_share "HYV"
	label var r_fert_share_total "Fertilizer"
	label var r_irr_share_total "Irrigation"
	
	* Conley SE
	ols_spatial_HAC r_`var' r_lang_control_post if cells_50km_stateborders == 1, lat(latitude) lon(longitude) timevar(year) panelvar(id) dist(`dist') dropvar bartlett
	
	outreg2 using "$results/tables/table_3.tex", keep(r_lang_control_post) tex(frag) dec(3) bracket label nor2 addtext(R-squared, "`R2'", Mean, "`control_mean'" , Cell f.e., Yes, Subdistrict border * Time f.e., Yes, District * Time f.e., Yes, Language * Time f.e., Yes) nocons nonotes stats(coef se) `replace'
	
	* Table C.3
	if "`var'" == "hyv_share" {
	** Table C.3: baseline
	outreg2 using "$results/tables/table_C3_`dist'.tex",  keep(r_lang_control_post) tex(frag) dec(3) bracket label nor2 addtext(R-squared, "`R2'", Mean, "`control_mean'" , Cell f.e., Yes, Subdistrict border * Time f.e., Yes, District * Time f.e., Yes, Language * Time f.e., Yes) nocons nonotes stats(coef se) asterisk(se) `replace'
	
	** Table C.2
	outreg2 using "$results/tables/table_C2_50km.tex",  keep(r_lang_control_post) tex(frag) dec(3) bracket label nor2 addtext(R-squared, "`R2'", Mean, "`control_mean'" , Cell f.e., Yes, Subdistrict border * Time f.e., Yes, District * Time f.e., Yes, Language * Time f.e., Yes) nocons nonotes stats(coef se) `replace'
	
	** Table C.3: alternative cutoff
	foreach cutoff in $alt_cutoff {  
	
		ols_spatial_HAC r_`var' r_lang_control_post if cells_50km_stateborders == 1, lat(latitude) lon(longitude) timevar(year) panelvar(id) dist(`cutoff') dropvar bartlett
		
		outreg2 using "$results/tables/table_C3_`cutoff'.tex",  keep(r_lang_control_post) tex(frag) dec(3) bracket label nor2 addtext(R-squared, "`R2'", Mean, "`control_mean'" , Cell f.e., Yes, Subdistrict border * Time f.e., Yes, District * Time f.e., Yes, Language * Time f.e., Yes) nocons nonotes stats(coef se) asterisk(se) `replace'
		
		}
	}		
}

************************* YIELD *************************
use "$data/yield_cell_input.dta", clear
keep if cells_50km_stateborders 

gen post = (year > 2007)
egen t = group(year)
gen lang_control_post = lang_control * post
label var lang_control_post "Lang Control * Post"


reghdfe total_yield_idt lang_control_post c.share_off_speakers#i.t if cells_50km_stateborders == 1 & cells_in_all == 1 [aw=pop2001], a(i.id i.subdist_border_id##i.t c.sharecrop_all_fao#i.t c.median_DIST_TOWN#i.t i.district_MCA_s##i.t c.POST_OFF#i.t i.majority_lang_group#i.t) cluster(tahsil_id) 
gen in_sample = e(sample) 
local R2 = round(e(r2), 0.001) 

unique majority_lang_group if in_sample == 1 & !missing(majority_lang_group)

* Residualize
foreach var in total_yield_idt ln_evi_delta_k ln_evi_max_k ln_evi_cumul_k lang_control_post {
	reghdfe `var' c.share_off_speakers#i.t if cells_50km_stateborders == 1 & cells_in_all == 1 & in_sample == 1 [aw=pop2001], a(i.id i.subdist_border_id##i.t c.sharecrop_all_fao#i.t c.median_DIST_TOWN#i.t i.district_MCA_s##i.t c.POST_OFF#i.t i.majority_lang_group#i.t) residuals(r_`var') cluster(tahsil_id) 
	replace r_`var' = sqrt(pop2001) * r_`var'
}


* Table 3
local var total_yield_idt
local dist = 10
label var r_lang_control_post "Non-state official language speakers (%) * Post"
label var r_total_yield_idt "Yield"
label var r_ln_evi_delta_k "EVI(Delta)"
label var r_ln_evi_max_k "EVI(Max)"
label var r_ln_evi_cumul_k "EVI(Cum)"
local replace append 

sum total_yield_idt if year <= 2007 & cells_50km_stateborders == 1 & in_sample [aw=pop2001]
local control_mean = round(`r(mean)', 0.001)


ols_spatial_HAC r_`var' r_lang_control_post if cells_50km_stateborders == 1 & cells_in_all == 1, lat(latitude) lon(longitude) timevar(year) panelvar(id) dist(`dist') dropvar bartlett

* Main panel
outreg2 using "$results/tables/table_3.tex", keep(r_lang_control_post) tex(frag) dec(3) bracket label nor2 addtext(R-squared, "`R2'", Mean, "`control_mean'" , Cell f.e., Yes, Subdistrict border * Time f.e., Yes, District * Time f.e., Yes, Language * Time f.e., Yes) nocons nonotes stats(coef se) `replace'

* Table C.3
outreg2 using "$results/tables/table_C3_`dist'.tex",  keep(r_lang_control_post) tex(frag) dec(3) bracket label nor2 addtext(R-squared, "`R2'", Mean, "`control_mean'" , Cell f.e., Yes, Subdistrict border * Time f.e., Yes, District * Time f.e., Yes, Language * Time f.e., Yes) nocons nonotes stats(coef se) asterisk(se) `replace'

** Table C.2 
outreg2 using "$results/tables/table_C2_50km.tex",  keep(r_lang_control_post) tex(frag) dec(3) bracket label nor2 addtext(R-squared, "`R2'", Mean, "`control_mean'" , Cell f.e., Yes, Subdistrict border * Time f.e., Yes, District * Time f.e., Yes, Language * Time f.e., Yes) nocons nonotes stats(coef se) `replace'

* Table C.1
outreg2 using "$results/tables/table_C1.tex", keep(r_lang_control_post) tex(frag) dec(3) bracket label nor2 addtext(R-squared, "`R2'", Mean, "`control_mean'", Cell f.e., Yes, Subdistrict border * Time f.e., Yes, District * Time f.e., Yes, Language * Time f.e., Yes) nocons nonotes stats(coef se) replace


* Table C.3: alternative cutoff
foreach cutoff in $alt_cutoff { 
	
	local var total_yield_idt
	
	ols_spatial_HAC r_`var' r_lang_control_post if cells_50km_stateborders == 1 & cells_in_all == 1, lat(latitude) lon(longitude) timevar(year) panelvar(id) dist(`cutoff') dropvar bartlett
	
	outreg2 using "$results/tables/table_C3_`cutoff'.tex", keep(r_lang_control_post) tex(frag) dec(3) bracket label nor2 addtext(R-squared, "`R2'", Mean, "`control_mean'" , Cell f.e., Yes, Subdistrict border * Time f.e., Yes, District * Time f.e., Yes, Language * Time f.e., Yes) nocons nonotes stats(coef se) asterisk(se) `replace'
}

****************** EVI MEASURES - APPENDIX TABLE (Table C.1) ******************
label var r_lang_control_post "Non-state official language speakers (%) * Post"
local replace append  
foreach c in ln_evi_delta_k ln_evi_max_k ln_evi_cumul_k {
	sum `c' if year <= 2007 & cells_50km_stateborders == 1 & in_sample  [aw=pop2001]
	local control_mean = round(`r(mean)', 0.001)
	
	local dist = 10 
	
	* R2
	reghdfe `c' lang_control_post c.share_off_speakers#i.t if cells_50km_stateborders == 1 & cells_in_all == 1 & in_sample == 1 [aw=pop2001], a(i.id i.subdist_border_id##i.t c.sharecrop_all_fao#i.t c.median_DIST_TOWN#i.t i.district_MCA_s##i.t c.POST_OFF#i.t i.majority_lang_group#i.t) cluster(tahsil_id) 
	local R2 = round(e(r2), 0.001)
	
	* Conley SE
	ols_spatial_HAC r_`c' r_lang_control_post if cells_50km_stateborders == 1, lat(latitude) lon(longitude) timevar(year) panelvar(id) dist(`dist') dropvar bartlett
	
	outreg2 using "$results/tables/table_C1.tex", keep(r_lang_control_post) tex(frag) dec(3) bracket label nor2 addtext(R-squared, "`R2'", Mean, "`control_mean'", Cell f.e., Yes, Subdistrict border * Time f.e., Yes, District * Time f.e., Yes, Language * Time f.e., Yes) nocons nonotes stats(coef se) `replace'

}

************************ Table C.4 
*************** LANG + RELIGION TOGETHER *********************
* Calls 
use "$data/calls_cell_input.dta", clear	
	keep if cells_50km_stateborders
	
	gen post = (year > 2007)
	egen t = group(year)
		
	gen lang_control_post = lang_control * post
	label var lang_control_post "Lang Control * Post"
		
		
	local replace replace 
	
	label var lang_control_post "Lang Control * Post"
	label var calls_all_per100C "All calls"
	label var calls_tech_per100C "Tech calls"
	label var calls_others_per100C "Other calls"
	
	
	
** Robustness to religion and language 
local c calls_all_per100C 
		
local replace replace 

local control_list "i.max_religion_group#i.t i.majority_lang_group#i.t"

foreach control in "`control_list'"{
	
	if "`control'" == "i.max_religion_group#i.t i.majority_lang_group#i.t" {
		local _rel "Yes"
	}
	else {
		local _rel "No"
	}
		
	if "`control'" == "i.max_religion_group#i.t i.majority_lang_group#i.t" {
		local _lang "Yes"
	}
	else {
		local _lang "No"
	}

	reghdfe `c' lang_control_post c.share_off_speakers#i.t if cells_50km_stateborders == 1, a(i.id i.subdist_border_id##i.t c.sharecrop_all_fao#i.t c.median_DIST_TOWN#i.t i.district_MCA_s##i.t c.POST_OFF#i.t "`control'") cluster(tahsil_id)
	
	sum `c' if e(sample)
	local control_mean = round(`r(mean)', 0.001)
	local R2 = round(e(r2), 0.001)

	* Residualize
	foreach var in calls_all_per100C lang_control_post {
		
		cap drop r_`var'
		
		reghdfe `var' c.share_off_speakers#i.t if cells_50km_stateborders == 1 , a(i.id i.subdist_border_id##i.t c.sharecrop_all_fao#i.t c.median_DIST_TOWN#i.t i.district_MCA_s##i.t c.POST_OFF#i.t "`control'") residuals(r_`var') cluster(tahsil_id) 
		
	}
	
	local dist = 10 
	
	local replace replace 
	label var r_lang_control_post "Non-state official language speakers (%) * Post"
	label var r_calls_all_per100C "All calls"

	local var calls_all_per100C
	
	* Conley SE
	ols_spatial_HAC r_`var' r_lang_control_post if cells_50km_stateborders == 1, lat(latitude) lon(longitude) timevar(year) panelvar(id) dist(`dist') dropvar bartlett
	
	outreg2 using "$results/tables/table_C4.tex", keep(r_lang_control_post) tex(frag) dec(3) bracket label nor2 addtext(R-squared, "`R2'", Mean, "`control_mean'", Cell f.e., Yes, Subdistrict border * Time f.e., Yes, District * Time f.e., Yes, Language * Time f.e., `_lang', Religion * Time f.e., `_rel') nocons nonotes stats(coef se) `replace'
	
	local replace append 

	}

* Tech adoption
use "$data/tech_cell_input.dta", clear
keep if cells_50km_stateborders

gen post = (year > 2007)
egen t = group(year)
sum post if year < 2008

	
gen lang_control_post = lang_control * post

label var lang_control_post "Lang Control * Post"


local c hyv_share 

local replace append 
		
local control_list "i.max_religion_group#i.t i.majority_lang_group#i.t"

foreach control in "`control_list'"{
		
	if "`control'" == "i.max_religion_group#i.t i.majority_lang_group#i.t" {
		local _rel "Yes"
	}
	else {
		local _rel "No"
	}
		
	if "`control'" == "i.max_religion_group#i.t i.majority_lang_group#i.t" {
		local _lang "Yes"
	}
	else {
		local _lang "No"
	}
		
	
	* R2 
	reghdfe `c' lang_control_post c.share_off_speakers#i.t if cells_50km_stateborders == 1 & dhyv_share2011 !=. & dhyv_share2016 !=. [aw=pop2001], a(i.id i.subdist_border_id##i.t c.sharecrop_all_fao#i.t c.median_DIST_TOWN#i.t i.district_MCA_s##i.t c.POST_OFF#i.t "`control'") cluster(tahsil_id)
	
	local R2 = round(e(r2), 0.001)
	
	sum `c' if cells_50km_stateborders == 1 & dhyv_share2011 !=. & dhyv_share2016 !=. & year <= 2007 [aw=pop2001]
	local control_mean = round(`r(mean)', 0.001)	

	** Conley 
	foreach var in  hyv_share lang_control_post {
		
		cap drop r_`var'
		
		reghdfe `var' c.share_off_speakers#i.t if cells_50km_stateborders == 1 & dhyv_share2011 !=. & dhyv_share2016 !=. [aw=pop2001], a(i.id i.subdist_border_id##i.t c.sharecrop_all_fao#i.t c.median_DIST_TOWN#i.t i.district_MCA_s##i.t c.POST_OFF#i.t "`control'") residuals(r_`var') cluster(tahsil_id)
		
		replace r_`var' = sqrt(pop2001) * r_`var'
		
	}
	
	local dist = 10 
	label var r_lang_control_post "Non-state official language speakers (%) * Post"
	label var r_hyv_share "HYV Share"
	local replace append 
	
	local var hyv_share
	
	ols_spatial_HAC r_`var' r_lang_control_post if cells_50km_stateborders == 1, lat(latitude) lon(longitude) timevar(year) panelvar(id) dist(`dist') dropvar bartlett
	
	outreg2 using "$results/tables/table_C4.tex", keep(r_lang_control_post) tex(frag) dec(3) bracket label nor2 addtext(R-squared, "`R2'", Mean, "`control_mean'", Cell f.e., Yes, Subdistrict border * Time f.e., Yes, District * Time f.e., Yes, Language * Time f.e., `_lang', Religion * Time f.e., `_rel') nocons nonotes stats(coef se) `replace'
	
	local replace append 
	
}


*YIELD 	
use "$data/yield_cell_input.dta", clear
keep if cells_50km_stateborders

gen post = (year > 2007)
egen t = group(year)
gen lang_control_post = lang_control * post
label var lang_control_post "Lang Control * Post"


sum total_yield_idt if year <= 2007 & cells_50km_stateborders == 1 [aw=pop2001]
local control_mean = round(`r(mean)', 0.001)

* Robustness to religion and language 
local c total_yield_idt
	
local replace append 
	
local control_list  "i.max_religion_group#i.t i.majority_lang_group#i.t"

foreach control in "`control_list'"{
		
	if "`control'" == "i.max_religion_group#i.t i.majority_lang_group#i.t" {
		local _rel "Yes"
	}
	else {
		local _rel "No"
	}
		
	if "`control'" == "i.max_religion_group#i.t i.majority_lang_group#i.t" {
		local _lang "Yes"
	}
	else {
		local _lang "No"
	}
		
	* R2
	reghdfe `c' lang_control_post c.share_off_speakers#i.t if cells_50km_stateborders == 1 & cells_in_all == 1  [aw=pop2001], a(i.id i.subdist_border_id##i.t c.sharecrop_all_fao#i.t c.median_DIST_TOWN#i.t i.district_MCA_s##i.t c.POST_OFF#i.t "`control'") cluster(tahsil_id) 
	
	local R2 = round(e(r2), 0.001)
	
	sum `c' if cells_50km_stateborders == 1 & cells_in_all == 1 & year <= 2007 [aw=pop2001]
	local control_mean = round(`r(mean)', 0.001)
			
	foreach var in total_yield_idt lang_control_post {
		cap drop r_`var'
		reghdfe `var' c.share_off_speakers#i.t if cells_50km_stateborders == 1 & cells_in_all == 1  [aw=pop2001], a(i.id i.subdist_border_id##i.t c.sharecrop_all_fao#i.t c.median_DIST_TOWN#i.t i.district_MCA_s##i.t c.POST_OFF#i.t "`control'") residuals(r_`var') cluster(tahsil_id) 
		
		replace r_`var' = sqrt(pop2001) * r_`var'
	}

	local dist = 10
	label var r_lang_control_post "Non-state official language speakers (%) * Post"
	label var r_total_yield_idt "Yield"
	local replace append
	
	local var total_yield_idt
	
	* Conley 
	ols_spatial_HAC r_`var' r_lang_control_post if cells_50km_stateborders == 1 & cells_in_all == 1, lat(latitude) lon(longitude) timevar(year) panelvar(id) dist(`dist') dropvar bartlett
	
	outreg2 using "$results/tables/table_C4.tex", keep(r_lang_control_post) tex(frag) dec(3) bracket label nor2 addtext(R-squared, "`R2'", Mean, "`control_mean'", Cell f.e., Yes, Subdistrict border * Time f.e., Yes, District * Time f.e., Yes, Language * Time f.e., `_lang', Religion * Time f.e., `_rel') nocons nonotes stats(coef se) `replace'
	
}

